$$ui = $$module('jDev.ui', {
    
  _id: 'jDev',
  _height: 270,
  _topHeight: 47,
    
  _element: null,
  
  _defaultTab: 'console',
  _tabPanel: null,
  _tabLinks: {},
  
  _activeTab: null,
  _activeTabId: null,
  _activeTabElement: null,
  
  _firebugElement: null,
  
  _maximized: false,

  init: function() {
    $$ui.create();
    
    $$event.add(
        document, 
        jDev.isIE || jDev.isSafari ? 'keydown' : 'keypress', 
        $$ui.onKeyDown
      );

    //$$event.add(window, 'resize', $$ui.onResize);
    //$$event.add(window, 'scroll', $$ui.fixPosition);
    
    /*
    $$event.add(
        document.getElementById('title'), 
        'click', 
        $$ui.toggleSize
      );
    
    document.getElementById('btHide').href = 'javascript:$$ui.hide();';
    document.getElementById('btMax').href = 'javascript:$$ui.maximize();';
    document.getElementById('btMin').href = 'javascript:$$ui.minimize();';
    
    var links = document.getElementById('tabs').getElementsByTagName('a');
    for(var i=0, li; li=links[i]; i++) {
      var tab= li.attributes.tab.value;
      $$ui._tabLinks[tab] = li;
      li.href = 'javascript:$$ui.showTab("'+tab+'");';
    }
    
    $$ui._tabPanel = document.getElementById('tabPanel');
    $$ui._tabCtrls = document.getElementById('tabCtrls');
    /**/
    //jDev.console.init();
    //jDev.doc.init();
    //jDev.test.init();
    
    //setTimeout($$ui.show, 0);
    //setTimeout($$ui.draw, 50);
    //setTimeout($$ui._initDraw, 0);    
  },
  
  _test: function() {
    alert();
  },
  
  _initDraw: function() {
    //$$ui.showTab($$ui._defaultTab);
  },
  
  create: function() {
    window.newwindow = '';
    $$ui.popitup(jDev.sourceURL + 'jDev/view/default/ui.html');
  },
  
  popitup: function (url) {
    if (!newwindow.closed && newwindow.location) {
      newwindow.location.href = url;
    }
    else {
      newwindow=window.open(url,'name','height=250,width=1000,resizable');
      if (!newwindow.opener) newwindow.opener = self;
    }
    if (window.focus) {newwindow.focus()}
    return false;
  },
  
  createF: function() {
    var el = document.createElement('iframe');
    $$ui._element = el; 
    el.id = 'jDevFrame';
    el.src = jDev.sourceURL + 'jDev/view/default/ui.html';
    el.setAttribute("frameBorder", "0");
    // http://msdn.microsoft.com/en-us/library/aa275080(SQL.80).aspx 
    // max 2147483647
    el.style.zIndex = "1147483647";
    //el.style.zIndex = "2147483647";
    el.style.position = document.all ? "absolute" : "fixed";
    el.style.width = document.body.clientWidth + "px";
    el.style.left = "0";
    el.style.bottom = "-1px";
    el.style.visibility = "visible";    
    el.style.height = $$ui._height;
    /*
    el.setAttribute("frameBorder", "0");
    el.style.visibility = (frameVisible ? "visible" : "hidden");    
    el.style.zIndex = "2147483583";
    el.style.position = document.all ? "absolute" : "fixed";
    el.style.width = "100%";
    el.style.left = "0";
    el.style.bottom = "0";
    el.style.height = "200px";
    /**/
    
    document.body.appendChild(el);
  },
  
  oldcreate: function() {
    var el = $$ui._element = document.createElement('div');
    el.id = $$ui._id;
    el.innerHTML = $$ui._template;
    
    document.body.appendChild(el);
  },
  
  fixPosition: function(){
    var style = $$ui._element.style;
    var maxHeight = document.body.clientHeight;
    var height = $$ui._maximized ? maxHeight-1 : $$ui._height;
    
    style.top = maxHeight - height + document.body.scrollTop + 'px'; 
  },
  
  draw: function() {
    var style = $$ui._element.style;
    var height = $$ui._maximized ? document.body.clientHeight-1 : $$ui._height;
    
    style.height = height + 'px';
    
    var width = document.body.clientWidth;
    style.width = width + 'px';

  /*
    var activeTab = $$ui._activeTab;
    var style = $$ui._element.style;
    
    var height = $$ui._maximized ? document.body.clientHeight-1 : $$ui._height;
    var panelHeight = height - $$ui._topHeight;
    
    style.height = height + 'px';
    $$ui._tabPanel.style.height = panelHeight + 'px';

    var width = document.body.clientWidth;
    style.width = width + 'px';
    
    if (activeTab && activeTab.draw) {
      activeTab.draw(panelHeight, width);
    }
    /**/
  },
  
  show: function(){
    $$ui._element.style.display = 'block';
    
    var activeTab = $$ui._activeTab;
    if (activeTab && activeTab.focus) {
      activeTab.focus();     
    }
  },
  
  hide: function(){
    $$ui._element.style.display = 'none';
  },
  
  isVisible: function() {
    return jDev.getCSS($$ui._element, 'display') != 'none';
  },
  
  toggle: function(){
    if($$ui.isVisible()) {
      $$ui.hide();
    } else {
      $$ui.show();
    }
  },
  
  maximize: function() {
    if (!$$ui._maximized) {
      $$ui._element.className = 'JDMaximized',
      $$ui._maximized = true;
      setTimeout($$ui.draw, 0);
    }
  },
  
  minimize: function() {
    if ($$ui._maximized) {
      $$ui._element.className = '',
      $$ui._maximized = false;
      setTimeout($$ui.draw, 0);
      setTimeout($$ui.draw, 50);
    }
  },
  
  toggleSize: function() {
    if ($$ui._maximized) {
      $$ui.minimize();
    } else {
      $$ui.maximize();
    }
  }, 
  
  showTab: function(id) {
    var tab = jDev[id];
    
    if(tab) {
      
      var activeId = $$ui._activeTabId;
      if (id != activeId) {
        
        if(activeId) {
          $$ui._activeTabElement.className = 'JDTab';
          $$ui._tabLinks[activeId].className = '';
        }
        
        $$ui._tabLinks[id].className = 'JDActiveTab';
        
        var r = [], ri = 0, buttons = tab._buttons;
        if(buttons) {
          for(var i=0, bi; bi=buttons[i]; i++) {
            r[ri++] = '<a title="';
            r[ri++] = bi.title;
            r[ri++] = '" href="javascript:';
            r[ri++] = bi.action;
            r[ri++] = '">';
            r[ri++] = bi.caption;
            r[ri++] = '</a>';
          }
        }
        $$ui._tabCtrls.innerHTML = r.join('');          
        
        $$ui._activeTab = tab;
        $$ui._activeTabId = id;
        $$ui._activeTabElement = tab._element;
        
        tab._element.className = 'JDActiveTab';
        if (tab.draw) {
          setTimeout($$ui.draw, 0);
        }        
      }
      
      if($$ui.isVisible() && tab.focus) {
        tab.focus();
      }
      
    } else if(!tab) {
      $$console.error('Tab "'+id+'" não carregada.');
    }    
  },
  
  toggleFirebug: function() {
    if (typeof firebug != 'undefined' && jDev._firebugElement) {
      if (firebug.env.minimized) {
        jDev._firebugElement.style.display = 'block';
        jDev._firebugElement.style.top = '';
        firebug.win.maximize();
        firebug.el.left.console.input.element.focus();
      } else {
        firebug.win.minimize();
        jDev._firebugElement.style.display = 'none';
        jDev._firebugElement.style.top = '';
      }
    }
  },
  
  onResize: function() {
    setTimeout($$ui.draw, 0);
    setTimeout($$ui.draw, 50);
  },
  
  onKeyDown: function(event) {
    if (event.keyCode == 123) {
      if (event.shiftKey) {
        $$ui.toggle();
      } else {
        $$ui.toggleFirebug();
      }
    } else {
      return;
    }
    
    $$event.stop(event);
  }
  
});

jDev.onReady($$ui.init);